#/bin/sh

ICS_FILE="./test.ics"

rm -rf $ICS_FILE
touch $ICS_FILE

function fun_SetHeader()
{
	echo "BEGIN:VCALENDAR" > $ICS_FILE
	echo "PRODID:-//Microsoft Corporation//Outlook MIMEDIR//EN " >> $ICS_FILE
	echo "VERSION:2.0" >> $ICS_FILE
	echo "BEGIN:VEVENT" >> $ICS_FILE
}

function fun_SetEnd()
{
	echo "END:VEVENT" >> $ICS_FILE
	echo "END:VCALENDAR" >> $ICS_FILE
}

DateFormat="-u +%Y%m%d"
TimeFormat="-u +%H%M%S"

# 设置起始事件的开始和结束时间
function fun_SetStartEventDate()
{
	nowDate=$(date ${DateFormat})
	nowTime=$(date ${TimeFormat})
	endData=$(date -d "+30 min" ${DateFormat})
	endTime=$(date -d "+30 min" ${TimeFormat})
	#echo "${nowDate}T${nowTime}Z"
	echo "DTSTART:${nowDate}T${nowTime}Z" >> $ICS_FILE
	echo "DTEND:${endData}T${endTime}Z" >> $ICS_FILE
}

function fun_SetInfo()
{
	echo "LOCATION:AnyWhere" >> $ICS_FILE
	echo "CATEGORIES:Study!" >> $ICS_FILE
	# 正文
	echo "DESCRIPTION:${2}" >> $ICS_FILE
	# 标题
	echo "SUMMARY:${1}" >> $ICS_FILE
	# 事件 UID
	echo "UID:$(date ${DateFormat})@think3r--rember" >> $ICS_FILE
	echo "PRIORITY:3" >> $ICS_FILE
}

# 根据需要偏移的天数生成并返回需要的时间字符串
function fun_calcDate()
{
	startDate=$(date -d "$1" ${DateFormat})
	startTime=$(date -d "$1" ${TimeFormat})
	endData=$(date -d "$1" -d "+30 min" ${DateFormat})
	endTime=$(date -d "$1" -d "+30 min" ${TimeFormat})
	retStr="${startDate}T${startTime}Z/${endData}T${endTime}Z"
	echo "${retStr}"
	#return "${retStr}"
}

# 配置重复事件的重复时间
function fun_SetMulityEventDate()
{
	repeatTimeStr1=$(fun_calcDate "+1 hour")
	repeatTimeStr3=$(fun_calcDate "+3 hour")
	repeatTimeStr8=$(fun_calcDate "+8 hour")
	repeatTimeStr11=$(fun_calcDate "+1 day")
	repeatTimeStr33=$(fun_calcDate "+3 day")
	repeatTimeStr88=$(fun_calcDate "+8 day")

	echo "RDATE;VALUE=PERIOD:${repeatTimeStr1},${repeatTimeStr2},${repeatTimeStr8},${repeatTimeStr11},${repeatTimeStr33},${repeatTimeStr88}" >> $ICS_FILE
	#echo "${repeatTimeStr3}"
}

fun_SetHeader

fun_SetStartEventDate

fun_SetInfo ${1} ${2}

fun_SetMulityEventDate

fun_SetEnd



